/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package au.com.domlogic.pvc.xhtml.document;

import au.com.domlogic.pvc.core.document.CompositeDocument;
import au.com.domlogic.pvc.core.element.ConcretePVCElement;
import au.com.domlogic.pvc.core.element.PVCElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Domenic Figliomeni
 */
public class XHTMLStrictDocument extends CompositeDocument
        implements Serializable, Cloneable {

    private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY =
            DocumentBuilderFactory.newInstance();
    public static final String NAMESPACE = "http://www.w3.org/1999/xhtml";

    static {
        DOCUMENT_BUILDER_FACTORY.setNamespaceAware(true);
    }
    private PVCElement head;
    private PVCElement title;
    private PVCElement body;

    /**
     * Use this constructor to get an XHTMLStrictDocument object from a document.
     * This constructor does no validation and may result in an object which
     * has nulls for the head, title or body and is in fact not an XHTML Document
     * at all.  This may change, but for the moment I will rely on the developer
     * to ensure a valid XHTML Document is passed to this constructor.
     * @param document
     */
    public XHTMLStrictDocument(Document document) {
        super(document);
        if (document.getElementsByTagName("head") != null) {
            this.head = new ConcretePVCElement(
                    (Element) document.getElementsByTagName("head").item(0));
        }
        if (document.getElementsByTagName("title") != null) {
            this.title = new ConcretePVCElement(
                    (Element) document.getElementsByTagName("title").item(0));
        }
        if (document.getElementsByTagName("body") != null) {
            this.body = new ConcretePVCElement(
                    (Element) document.getElementsByTagName("body").item(0));
        }
    }

    @Override
    public String getIdAttrName() {
        return "id";
    }

    /**
     * TODO: Test this method
     * @param document
     * @return A vector holding all the id attribute values in the document
     */
    public static List<String> getExistingIds(Document document) {
        List<String> result = new ArrayList<String>();
        try {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile("//@id");
            NodeList idAttrs = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
            for (int i = 0; i < idAttrs.getLength(); i++) {
                Attr attr = (Attr) idAttrs.item(i);
                result.add(attr.getValue());
            }
        } catch (XPathExpressionException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @Override
    public XHTMLStrictDocument clone() throws CloneNotSupportedException {
        return (XHTMLStrictDocument) super.clone();
    }

    @Override
    public boolean equals(Object obj) {
        boolean result = false;
        if (obj instanceof XHTMLStrictDocument) {
            result = super.equals(obj);
        }
        return result;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public String toString() {
        return super.toString();
    }

    /**
     * @return the head
     */
    public PVCElement getHead() {
        return head;
    }

    /**
     * @return the title
     */
    public PVCElement getTitle() {
        return title;
    }

    /**
     * @return the body
     */
    public PVCElement getBody() {
        return body;
    }
}
